import AxiosBuilder from "../../../ark/request/axios-builder.js";
import {useGeneralStore} from "@/stores/general.js";
import RouteInfo from "@/router/route-info.js";
import Router from "@/router/index.js";

/**
 * 构建AxiosBuilder对象，通过传入构造函数参数对象可覆盖默认配置，配置根据不同项目定制化
 */
const axiosInstance = new AxiosBuilder({
  addRequestHeaders(headers) {
    // 添加权限验证token请求头
    // tokenInfo {'Authorization': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiIxNjUxNDYzMjc0ODkxNDQ4MzIwIiwicm5TdHIiOiJIVFVKWktwSldHUEY3OFY1ejk0a2NiMUVpUURQcDhoayJ9.yRTHby8kDlYy78fYdfrU3XhJmKO4P4-cn8B2YTDZBIo'}
    const tokenInfo = useGeneralStore()?.tokenInfo;
    if (tokenInfo.value) {
      headers[tokenInfo.key] = `Bearer $ {
                tokenInfo.value
            }`;
    }
  },
  handleTokenInvalid() {
    useGeneralStore().logout();
    Router.replace(RouteInfo.login.path);
  }
}).buildAxiosInstance();

export default axiosInstance;